<!DOCTYPE html>
<html>
<head>
    {include file="_Fragment/meta" /}
    <link rel="stylesheet" href="/static/pear/component/pear/css/pear.css" />
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <table id="user-table" lay-filter="user-table"></table>
    </div>
</div>

<script type="text/html" id="user-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        选择课程
    </button>
</script>


<script type="text/html" id="user-bar">
    <button class="pear-btn pear-btn-sm" id="more_{{d.id}}"><i class="layui-icon layui-icon-triangle-d"></i></button>
</script>

<script type="text/html" id="table-wait">
    <input type="checkbox" name="wait" value="{{d.aid}}" lay-skin="switch" lay-text="开启|关闭" lay-filter="table-wait" data-json="{{ encodeURIComponent(JSON.stringify(d)) }}" {{ d.wait == 2 ? 'checked' : '' }}>
</script>
{include file="_Fragment/javascript" /}
<script>
    layui.use(['table', 'form', 'jquery', 'common', 'dropdown'], function() {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let common = layui.common;
        let dropdown = layui.dropdown;
        let flex_id = "{$flex_id}";
        let cate_id = "{$cate_id}";
        let cols = [
            [{
                    title: 'ID',
                    field: 'aid',
                    width:'10%',
                    align: 'center',
                    fixed: 'left',
                },
                {
                    title: '排序',
                    field: 'sort',
                    align: 'center',
                    edit:'text',
                    width:'10%',
                    fixed: 'left',
                },
                {
                    title: 'CourseId',
                    field: 'courseid',
                    align: 'center',
                    width:'10%',
                    fixed: 'left',
                },
                {
                    title: '课程名称',
                    field: 'seminar',
                    align: 'center',
                    width:'20%',
                    fixed: 'left',
                },
                {
                    title: '等候名单',
                    field: 'wait',
                    align: 'center',
                    templet:'#table-wait',
                    width:'10%'
                },
                {
                    title: '课程属性',
                    field: 'course_type_name',
                    align: 'center',
                    width:'10%'
                },
                {
                    title: '课程类型',
                    field: 'module',
                    align: 'center',
                    width:'10%'
                },
                {
                    title: '课程语言',
                    field: 'english',
                    align: 'center',
                    width:'10%'
                },
                {
                    title: '课程席位',
                    field: 'xiwei',
                    align: 'center',
                    width:'10%'
                },
                {
                    title: '操作',
                    toolbar: '#user-bar',
                    align: 'center',
                    width: 130
                }
            ]
        ]

        table.render({
            elem: '#user-table',
            url: '/Flex/getCateCourseList?flex_id='+flex_id+'&cate_id='+cate_id,
            page: {layout:['count', 'prev', 'page', 'next', 'skip']},
            limit: 10,
            cols: cols,
            //skin: 'line',
            toolbar: '#user-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter'],
            done: function(res, curr, count) {
                for (var i = 0; i < res.data.length; i++) {

                    if (res.data[i].cate_type == 1) {
                        var datas = [{
                            title: '编辑班级',
                            courseid: res.data[i].courseid,
                            type: 1,
                        }, {
                            title: '编辑课程',
                            id: res.data[i].id,
                            type: 2
                        }, {
                            title: '可见范围',
                            courseid: res.data[i].courseid,
                            type: 3
                        }, {
                            title: '软推荐',
                            id: res.data[i].id,
                            courseid: res.data[i].courseid,
                            type: 4
                        }];
                    } else {

                        var datas = [{
                            title: '编辑班级',
                            courseid: res.data[i].courseid,
                            type: 1,
                        }, {
                            title: '编辑课程',
                            id: res.data[i].id,
                            type: 2
                        }];
                    }

                    dropdown.render({
                        elem: '#more_' + res.data[i].id,
                        data: datas,
                        id: '#more_' + res.data[i].id,
                        click: function(obj) {
                            if (obj.type == 1) {
                                parent.layui.admin.addTab('course_classes'+obj.courseid, obj.courseid+"编辑班级", '/Flex/classes?cate_id='+cate_id+'&flex_id='+flex_id+'&courseid='+obj.courseid)
                            } else if(obj.type == 2) {
                                layer.open({
                                    type: 2,
                                    title: '编辑课程',
                                    shade: 0.1,
                                    area: [common.isModile()?'100%':'900px', common.isModile()?'100%':'600px'],
                                    content: '/Course/edit?id='+obj.id
                                });
                            } else if(obj.type == 3) {
                                parent.layui.admin.addTab(obj.courseid, obj.courseid+"可见范围", '/Course/recommend?courseid='+obj.courseid+'&type=1')
                            } else if(obj.type == 4) {
                                parent.layui.admin.addTab('22'+obj.id, obj.courseid+"软推荐", '/Course/recommend?courseid='+obj.courseid+'&type=2')
                            }
                        }
                    });
                }
            }
        });
        form.on('submit(user-query)', function(data) {
            table.reload('user-table', {
                where: data.field
                ,page: {
                    curr: 1, //重新从第 1 页开始
                    layout:['count', 'prev', 'page', 'next', 'skip']
                }
                ,limit: 10,
            })
            return false;
        });


        //监听锁定操作
        form.on('switch(table-wait)', function(obj) {
            $.post("{:url('setCateCourseWait')}", {id: this.value}, function (result) {
                if(result.status != 0) {
                    layer.tips(result.msg, obj.othis);
                }

            });
        });


        //监听单元格编辑
        table.on('edit(user-table)', function(obj){
            var value = obj.value //得到修改后的值
                ,data = obj.data //得到所在行所有键值
            $.post("{:url('setCateCourseSort')}", {id: data.aid,sort:value}, function (result) {
                if(result.status == 0) {
                    table.reload("user-table");
                } else {
                    layer.msg(result.msg,{icon:2,time:1000});
                    table.reload("user-table");
                }
            });
        });

        table.on('toolbar(user-table)', function(obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            }
        });



        window.add = function() {
            layer.open({
                type: 2,
                title: '选择课程',
                shade: 0.1,
                area: [common.isModile()?'100%':'900px', common.isModile()?'100%':'600px'],
                content: '/Flex/xuanke?flex_id='+flex_id+'&cate_id='+cate_id
            });
        }

        window.refresh = function(param) {
            table.reload('user-table');
        }
    })
</script>
</body>
</html>
